package com.qrxl.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qrxl.common.JsonResult;
import com.qrxl.common.controller.BaseController;
import com.qrxl.common.vo.BaseQuery;
import com.qrxl.common.vo.req.IdReq;
import com.qrxl.user.entity.Role;
import com.qrxl.user.entity.RoleMenu;
import com.qrxl.user.service.IRoleMenuService;
import com.qrxl.user.service.IRoleService;
import com.qrxl.user.vo.req.RoleAddReq;
import com.qrxl.user.vo.req.RoleListReq;
import com.qrxl.user.vo.req.RoleRelMenuReq;
import com.qrxl.user.vo.req.RoleUpdateReq;
import com.qrxl.user.vo.res.RoleListRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author friday
 * @since 2020-12-05
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user/role")
public class RoleController extends BaseController {
    @Autowired
    IRoleService roleService;
    @Autowired
    IRoleMenuService roleMenuService;

    @ResponseBody
    @PostMapping("/v1/add")
    @ApiOperation(value = "角色新增")
    public JsonResult register(@RequestBody @Validated RoleAddReq req) {
        roleService.add(req);
        return JsonResult.genJsonResultByOk();
    }

    @ResponseBody
    @GetMapping("/v1/queryList")
    @ApiOperation(value = "角色列表")
    public JsonResult<IPage<RoleListRes>> list(@Validated BaseQuery baseQuery, RoleListReq req) {
        Role cond = new Role();
        BeanUtils.copyProperties(req, cond);
        //只允许查看本平台的角色
        return JsonResult.genJsonResultByOk(roleService.selectPage(baseQuery, cond));
    }

    @ResponseBody
    @PostMapping("/v1/update")
    @ApiOperation(value = "角色编辑")
    public JsonResult update(@Validated @RequestBody RoleUpdateReq req) {
        //只允许修改自己创建的角色
        roleService.updateVo(req, getUserLoginInfo());
        return JsonResult.genJsonResultByOk();
    }

    @ResponseBody
    @PostMapping("/v1/delete")
    @ApiOperation(value = "角色删除")
    public JsonResult delete(@Validated @RequestBody IdReq req) {
        roleService.delete(req, getUserLoginInfo());
        return JsonResult.genJsonResultByOk();
    }

    @ResponseBody
    @PostMapping("/v1/relMenus")
    @ApiOperation(value = "角色关联菜单")
    public JsonResult relMenus(@Validated @RequestBody RoleRelMenuReq req) {
        roleMenuService.relMenus(req, getUserLoginInfo());
        return JsonResult.genJsonResultByOk();
    }

    @ResponseBody
    @GetMapping("/v1/roleMenuIdList")
    @ApiOperation(value = "角色ID获取关联的菜单")
    public JsonResult roleMenuIdList(@Validated IdReq req) {
        RoleMenu cond = new RoleMenu();
        cond.setRoleId(req.getId());
        List<String> menuIList = roleMenuService.list(Wrappers.query(cond)).stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        return JsonResult.genJsonResultByOk(menuIList);
    }

}

